Text to hash
import hashlib
def get_hash(text, algorithm='sha256'):
try:
hash_func = getattr(hashlib, algorithm)
hash_obj = hash_func(text.encode('utf-8'))
return hash_obj.hexdigest()
except AttributeError:
return "Unsupported hash algorithm."
def main():
print("--- Text to Hash Converter ---")
text = input("Enter text to hash: ")
print("Choose hash algorithm:")
print("1. MD5")
print("2. SHA-1")
print("3. SHA-256")
print("4. SHA-512")
choice = input("Your choice (1-4): ")
algo_map = {
'1': 'md5',
'2': 'sha1',
'3': 'sha256',
'4': 'sha512'
}
algorithm = algo_map.get(choice, 'sha256')
hashed_text = get_hash(text, algorithm)
print(f"{algorithm.upper()} hash:\n{hashed_text}")
if __name__ == "__main__":
main()
Code output
--- Text to Hash Converter ---
Enter text to hash: hello
Choose hash algorithm:
1. MD5
2. SHA-1
3. SHA-256
4. SHA-512
Your choice (1-4): 3
SHA256 hash:
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824